package com.ysd.lis.service.iqc.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.iqc.*;
import com.ysd.lis.entity.lab.LabInstrSetup;
import com.ysd.lis.entity.lab.LabManufacturer;
import com.ysd.lis.entity.qc.QcMaterial;
import com.ysd.lis.mapper.iqc.LimsIQCMaterialBatchesMapper;
import com.ysd.lis.mapper.lab.LabInstrSetupMapper;
import com.ysd.lis.mapper.lab.LabManufacturerMapper;
import com.ysd.lis.mapper.qc.QcMaterialMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.iqc.LimsIQCMaterialBatchesService;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-01-08
 */
@Service
@Slf4j
public class LimsIQCMaterialBatchesServiceImpl extends ServiceImpl<LimsIQCMaterialBatchesMapper, LimsIQCMaterialBatches> implements LimsIQCMaterialBatchesService {

    @Autowired
    LimsIQCMaterialBatchesMapper limsIQCMaterialBatchesMapper;
    @Autowired
    LabInstrSetupMapper labInstrSetupMapper;
    @Autowired
    LabManufacturerMapper labManufacturerMapper;
    @Autowired
    QcMaterialMapper qcMaterialMapper;
    @Override
    public Result syncQcMaterial(CommonPageDto commonPageDto) {
        List<LimsIQCMaterialBatches> gdbLisMaterialInfos = this.getGdbLisMaterialInfo();
        System.out.println(gdbLisMaterialInfos);
        List<String> instrCodeList = gdbLisMaterialInfos.stream().map(LimsIQCMaterialBatches::getInstrCode).distinct().collect(Collectors.toList());
        //根据code查询仪器
        MPJLambdaWrapper<LabInstrSetup> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(LabInstrSetup::getDelFlag,0)
                .in(LabInstrSetup::getInstrCode,instrCodeList);
        List<LabInstrSetup> instrSetupList = labInstrSetupMapper.selectList(queryWrapper);
        //根据code查出厂商
        List<String> manufacturerCodeList = gdbLisMaterialInfos.stream().map(LimsIQCMaterialBatches::getManufacturerCode).distinct().collect(Collectors.toList());
        MPJLambdaWrapper<LabManufacturer> queryWrapper1 = new MPJLambdaWrapper<>();
        queryWrapper1.eq(LabManufacturer::getDelFlag,0)
                .in(LabManufacturer::getManuCode,manufacturerCodeList);
        List<LabManufacturer> manufacturerList = labManufacturerMapper.selectList(queryWrapper1);


        //仪器，批号，水平 确定一条质控物
        for(LimsIQCMaterialBatches iqcm : gdbLisMaterialInfos){

            String instrCode = iqcm.getInstrCode();
            List<LabInstrSetup> collect = instrSetupList.stream().filter(a -> a.getInstrCode().equals(instrCode)).collect(Collectors.toList());
            LabInstrSetup labInstrSetup = collect.get(0);

            //仪器，批号，水平 确定一条质控物
            LambdaQueryWrapper<QcMaterial> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(QcMaterial::getInstrId,labInstrSetup.getId())
                    .eq(QcMaterial::getBatchNum,iqcm.getBatchNumber())
                    .eq(QcMaterial::getHorizontal,iqcm.getHorizontal())
                    .eq(QcMaterial::getDelFlag,0);
            int count = qcMaterialMapper.selectCount(queryWrapper2);
            if(count<=0){

                String manufacturerCode = iqcm.getManufacturerCode();
                List<LabManufacturer> collect2 = manufacturerList.stream().filter(a -> a.getManuCode().equals(manufacturerCode)).collect(Collectors.toList());
                LabManufacturer labManufacturer = collect2.get(0);

                QcMaterial qcMaterial = new QcMaterial();

                qcMaterial.setInstrId(labInstrSetup.getId());
                qcMaterial.setQcNo(iqcm.getQcNo());
                qcMaterial.setMaterialName(iqcm.getMaterialName());
                qcMaterial.setBatchNum(iqcm.getBatchNumber());
                qcMaterial.setHorizontal(iqcm.getHorizontal());
                if (ToolsUtils.isNotEmpty(labManufacturer)){
                    qcMaterial.setManufacturer(labManufacturer.getId());
                }
                qcMaterial.setEnableDate(iqcm.getEnableTime());
                qcMaterial.setValidDate(iqcm.getExpiryDate());
                qcMaterial.setStopDate(iqcm.getDisableTime());
                qcMaterial.setEntryDate(new Timestamp(new Date().getTime()));
                qcMaterial.setSeq(1);
                qcMaterial.setEntryOperator("1");
                qcMaterial.setReportBasis(true);
                int insert = qcMaterialMapper.insert(qcMaterial);
                System.out.println(insert);
            }

        }

        return null;
    }

    public List<LimsIQCMaterialBatches> getGdbLisMaterialInfo(){
        MPJLambdaWrapper<LimsIQCMaterialBatches> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LimsIQCMaterialBatches.class)
                .selectAs("ins",LimsInss::getCode,LimsIQCMaterialBatches::getInstrCode)
                .selectAs("qm",LimsIQCMaterials::getQcNo,LimsIQCMaterialBatches::getQcNo)
                .selectAs("qm",LimsIQCMaterials::getName,LimsIQCMaterialBatches::getMaterialName)
                .selectAs("le",LimsIQCMedicineDecisionLevels::getCode,LimsIQCMaterialBatches::getHorizontal)
                .selectAs("rf",RmsManufacturers::getCode,LimsIQCMaterialBatches::getManufacturerCode)
                .leftJoin(LimsIQCMaterials.class,"qm",p->p.eq(LimsIQCMaterials::getId,LimsIQCMaterialBatches::getIQCMaterialId))
                .leftJoin(LimsInss.class,"ins",p->p.eq(LimsInss::getId,LimsIQCMaterials::getInsId))
                .leftJoin(LimsIQCMedicineDecisionLevels.class,"le",p->p.eq(LimsIQCMedicineDecisionLevels::getId,LimsIQCMaterials::getIQCMedicineDecisionLevelId))
                .leftJoin(RmsManufacturers.class,"rf", p->p.eq(RmsManufacturers::getId,LimsIQCMaterialBatches::getManufacturerId))
                .eq(LimsIQCMaterialBatches::getIsDeleted,0)
                .eq(LimsIQCMaterials::getIsDeleted,0);

        List<LimsIQCMaterialBatches> limsIQCMaterialBatches = limsIQCMaterialBatchesMapper.selectList(queryWrapper);

        return limsIQCMaterialBatches;

    }



}
